import {useAccountStore} from "./AccountStore.ts";
import {defineStore} from "pinia";
import {CacheMap, useCache} from "../utils/CacheUtils.ts";
import type {OldName, TinyUserInfo, UserDetail} from "../network/types/User.ts";
import {BlockConfig} from "../network/types/BlockConfig.ts";

const api = () => useAccountStore().getNgaApi().user

export const replaceAvatar = (url: string) => url
    .replace('https://img.nga.178.com/', '/static/attach/')
    .replace('https://img.nga.cn/', '/static/attach/')

export const useUserStore = defineStore('UserStore', {
    state: () => ({
        cache: new CacheMap<UserDetail>(),
        oldNameCache: new CacheMap<OldName[]>(),
        blockConfigCache: new CacheMap<BlockConfig>(),
        blockConfig: new BlockConfig(""),
    }),
    actions: {
        detail(userId: number | undefined, username: string | undefined, force?: boolean) {
            const key = `user detail ${userId} ${username}`
            return useCache(this.cache, key, force || false, () => api().info(userId, username).then(res => {
                res.avatars = res.avatars && res.avatars.map(i => replaceAvatar(i))
                return res
            }))
        },
        detailHtml(userId: number | undefined, username: string | undefined, force?: boolean) {
            const key = `user detail html ${userId} ${username}`
            return useCache(this.cache, key, force || false, () => api().infoHtml(userId, username).then(res => {
                res.avatars = res.avatars && res.avatars.map(i => replaceAvatar(i))
                return res
            }))
        },
        detailHtmlText(text: string, force?: boolean) {
            const userId = Number(text)
            if (isNaN(userId)) {
                return useUserStore().detailHtml(undefined, text, force)
            }
            return useUserStore().detailHtml(userId, undefined, force)
        },

        oldName(userId: number, force?: boolean) {
            const key = `old name ${userId}`
            return useCache(this.oldNameCache, key, force || false, () => api().oldName(userId))
        },

        async blockConfigGet(force?: boolean) {
            const userId = useAccountStore().getAccountUserId();
            const key = `${userId}`
            return useCache(this.blockConfigCache, key, force || false, () => api().blockConfigGet(userId), 10 * 60).then(res => {
                this.blockConfig = res
                return res
            })
        },
        async blockConfigSet(config: BlockConfig) {
            return api().blockConfigSet(config).then(res => {
                this.blockConfigGet(true)
                return res
            })
        },
        addBlockUser(user: TinyUserInfo) {
            const users: TinyUserInfo[] = JSON.parse(JSON.stringify(this.blockConfig.users))
            users.push(user)
            const param = new BlockConfig()
            param.keywords = this.blockConfig.keywords
            param.users = users
            return this.blockConfigSet(param)
        },
        delBlockUser(userId: number) {
            const users = this.blockConfig.users.filter(it => it.userId !== userId)
            const param = new BlockConfig()
            param.keywords = this.blockConfig.keywords
            param.users = users
            return this.blockConfigSet(param)
        },
        // 设置签名
        signatureSet(uid: number, signature: string) {
            return api().signatureSet(uid, signature)
        }
    },
})